﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Query : System.Web.UI.Page
{

    public int AddAndUpdateSign = 0;
    public string urls = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        string urlId1 = Request.QueryString["UrlID"];
        int intURLID1 = int.Parse(urlId1);
        DAL.UrlDALs urldals = new DAL.UrlDALs();
        urls = urldals.GetByID(intURLID1).Url;


        if (!Page.IsPostBack)
        {
            string urlId = Request.QueryString["UrlID"];
            int intURLID = 0;
            DAL.QueryDALs querydal = new DAL.QueryDALs();
            if (urlId != null && urlId != "")
            {
                intURLID = int.Parse(urlId);
                this.gdvList.DataSource = querydal.GetByUrlID(intURLID);
                string[] keys ={ "QueryId" };
                this.gdvList.DataKeyNames = keys;
                this.gdvList.DataBind();
            }
            else
            {
                this.gdvList.DataSource = querydal.GetAll();
                string[] keys ={ "QueryId" };
                this.gdvList.DataKeyNames = keys;
                this.gdvList.DataBind();
            }
        }
        else
        {
            string urlId = Request.QueryString["UrlID"];
            int intURLID = 0;
            DAL.QueryDALs querydal = new DAL.QueryDALs();
            if (urlId != null && urlId != "")
            {
                intURLID = int.Parse(urlId);
                this.gdvList.DataSource = querydal.GetByUrlID(intURLID);
                string[] keys ={ "QueryId" };
                this.gdvList.DataKeyNames = keys;
                this.gdvList.DataBind();
            }
            else
            {
                this.gdvList.DataSource = querydal.GetAll();
                string[] keys ={ "QueryId" };
                this.gdvList.DataKeyNames = keys;
                this.gdvList.DataBind();
            }

        }
         
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlType.SelectedValue)
        {
           //下载时间
            case "0":
                this.txtLeft.Enabled = false;
                this.txtLeft.Text = string.Empty;
                lblUnit.Text = "秒";
                break;
            case "1":
                this.txtLeft.Enabled = false;
                this.txtLeft.Text = string.Empty;
                lblUnit.Text = "K";
                break;
            case "2":
                this.txtLeft.Enabled = true;
                this.txtLeft.Text = "输入正则表达式";
                lblUnit.Text = "";
                break;
            case "3":
                this.txtLeft.Enabled = true;
                this.txtLeft.Text = @"前辍(?<value>\d+)后辍";
                lblUnit.Text = "";
                break;
            case "4":
                this.txtLeft.Enabled = true;
                this.txtLeft.Text = @"前辍(?<value>\d+)后辍";
                lblUnit.Text = "";
                break;

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["UrlID"] == null)
        {
            Response.Write("<script>alert('URL错误');</script>");

            return;
        }
        DAL.QueryDALs querydal = new DAL.QueryDALs();

        Entities.Query query = new Entities.Query();
        query.LeftValue = txtLeft.Text.Trim();
        query.Right = txtRegith.Text.Trim();
        query.UrlID = Convert.ToInt16(Request.QueryString["UrlID"]);
        query.CompareType = ddlComparType.SelectedValue.ToString();
        query.Type = ddlType.SelectedValue.ToString();

        querydal.Insert(query);

        this.gdvList.DataSource = querydal.GetAll();
        string[] keys ={ "QueryId" };
        this.gdvList.DataKeyNames = keys;
        this.gdvList.DataBind();
        Response.Redirect("Query.aspx?UrlID=" + query.UrlID);
    }
    protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.NewEditIndex].Value);
        DAL.QueryDALs querydal = new DAL.QueryDALs();
        Entities.Query query = querydal.GetByID(id);

        ddlType.SelectedItem.Selected = false;
        foreach (ListItem item in ddlType.Items)
        {
            if (query.Type == item.Value)
            {
                item.Selected = true;
            }
        }

        ddlComparType.SelectedItem.Selected = false;
        foreach (ListItem item in this.ddlComparType.Items)
        {
            if (query.CompareType == item.Value)
            {
                item.Selected = true;
            }
        }


        txtLeft.Text = query.LeftValue;
        txtRegith.Text = query.Right;


        AddAndUpdateSign = 1;
        HidQueryId.Value = id.ToString();
    }
    protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.RowIndex].Value);
        DAL.QueryDALs querydal = new DAL.QueryDALs();
        int urlid = querydal.GetByID(id).UrlID;
        Entities.Query query = querydal.GetByID(id);
        querydal.DeleteQuerys(id);

        //this.gdvList.DataSource = querydal.GetAll();
        //string[] keys ={ "QueryId" };
        //this.gdvList.DataKeyNames = keys;
        //this.gdvList.DataBind();

        Response.Redirect("Query.aspx?UrlID=" + urlid);

    }

    protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvList.PageIndex = e.NewPageIndex;
        DAL.QueryDALs querydal = new DAL.QueryDALs();
        this.gdvList.DataSource = querydal.GetAll();
        string[] keys ={ "QueryId" };
        this.gdvList.DataKeyNames = keys;
        this.gdvList.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(HidQueryId.Value);

        DAL.QueryDALs querydal = new DAL.QueryDALs();

        Entities.Query query = querydal.GetByID(id);
        query.LeftValue = txtLeft.Text.Trim();
        query.Right = txtRegith.Text.Trim();
        query.UrlID = Convert.ToInt16(Request.QueryString["UrlID"]);
        query.CompareType = ddlComparType.SelectedValue.ToString();
        query.Type = ddlType.SelectedValue.ToString();

        querydal.Update(query);

        //this.gdvList.DataSource = querydal.GetAll();
        //string[] keys ={ "QueryId" };
        //this.gdvList.DataKeyNames = keys;
        //this.gdvList.DataBind();
        Response.Redirect("Query.aspx?UrlID=" + query.UrlID);
    }


    protected string DGFormatSex1(string xb)
    {
        if (xb == "0")
            return "下载时间验证";
        else if (xb == "1")
            return "文件尺寸验证";
        else if (xb == "2")
            return "字符串是否存在验证";
        else if (xb == "3")
            return "数字比较";
        else if (xb == "4")
            return "字符串重复比较";

        return "";
        
    }


    protected string DGFormatSex2(string xb)
    {
        if (xb == "0")
            return "等于";
        else if (xb == "1")
            return "不等于";
        else if (xb == "2")
            return "小于";
        else if (xb == "3")
            return "大于";
        else if (xb == "4")
            return "小于等于";
        else if (xb == "5")
            return "大于等于";

        return "";
    }
}
